#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
采用BeautifulSoup解析html并获取内容
注解
"""

__author__ = 'hubert'

from bs4 import BeautifulSoup
import requests

import logging
logging.basicConfig(level=logging.INFO)


class Movie(object):
    def __init__(self):
        # 中文名称
        self.name = ''
        # 外文名称
        self.foreignname = ''
        # 导演
        self.director = ''
        # 演员
        self.actor = ''
        # 编剧
        self.screenwriter = ''
        # 出品时间
        self.releaseyear = ''
        # 地区
        self.area = ''
        # 类型
        self.type = ''
        # 片长
        self.duration = ''


def __parser_douban__(url):
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
                             'Chrome/52.0.2743.116 Safari/537.36 Edge/15.15063'}
    r = requests.get(url, headers=headers)
    soup = BeautifulSoup(r.text, 'lxml')
    return soup


# 百度百科有302跳转问题,通过设置session保持及header来解决
def __parser_iqiyi__(url):
    headers = {'Accept': 'text/html, application/xhtml+xml, image/jxr, */*',
               'Accept - Encoding': 'gzip, deflate',
               'Accept-Language': 'zh-Hans-CN, zh-Hans; q=0.5',
               'Connection': 'Keep-Alive',
               'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
                             'Chrome/52.0.2743.116 Safari/537.36 Edge/15.15063',
               'X-Requested-With': 'XMLHttpRequest'}
    movie = Movie()
    s = requests.Session()
    r = s.get(url, headers=headers)
    # 设置编码为utf8
    r.encoding = 'uft-8'
    soup = BeautifulSoup(r.text, 'lxml')

    print(soup)

    #dl_left = soup.find(class_='qy-player-detail-pop')
    #print(dl_left)

    return movie


def __parser_youkuvideo__(url):
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.128 Safari/537.36'}
    s = requests.Session()
    r = s.get(url, headers=headers)
    # 设置编码为utf8
    r.encoding = 'uft-8'
    soup = BeautifulSoup(r.text, 'lxml')
    # print(soup)
    # 优酷详情页导演、演员信息在script标签中2021.7.23
    li_script = soup.findAll("script")
    print(li_script)
    return soup


def excel_insert(sheet,moviedata,row):
    sheet.write(row, '', '')


# pip安装第三方组件失败时，可采用离线安装方式
# python -m pip install lxml-4.2.3-cp37-cp37m-win_amd64.whl
if __name__ == '__main__':
    # 电视剧详情页
    url = 'https://v.youku.com/v_show/id_XNTE2NjE1OTEzMg==.html'


    soup = __parser_youkuvideo__(url)


